import { loginByLoginName, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
      user: '',
      status: '',
      token: getToken(),
      name: '',
      loginName:'',
      phone: '',
      roles: [],
      setting: {
        articlePlatform: []
      },
      staffId:'',
      companyId:'',
      companyName:'',
    companyLogo:''
  },

  mutations: {
      SET_TOKEN: (state, token) => {
        state.token = token
      },
      SET_SETTING: (state, setting) => {
        state.setting = setting
      },
      SET_STATUS: (state, status) => {
        state.status = status
      },
      SET_LOGIN_NAME:(state,loginName)=>{
          state.loginName = loginName
          sessionStorage.setItem("loginName",loginName)
      },
      SET_COMPANY_ID:(state,companyId)=>{
          state.companyId = companyId
          sessionStorage.setItem("companyId",companyId)
      },
      SET_COMPANY_NAME:(state,companyName)=>{
          state.companyName = companyName
          sessionStorage.setItem("companyName",companyName)
      },
      SET_COMPANY_LOGO:(state,logo)=>{
          state.companyLogo = logo
          sessionStorage.setItem("companyLogo",logo)
      },
      SET_STAFF_ID: (state, staffId) => {
          state.staffId = staffId
          sessionStorage.setItem("staffId",staffId)
      },
      SET_PHONE: (state, phone) => {
          state.phone = phone
          sessionStorage.setItem("phone",phone)
      },
      SET_NAME: (state, name) => {
        state.name = name
          sessionStorage.setItem("name",name)
      },
      SET_ROLES: (state, roles) => {
        state.roles = roles
          sessionStorage.setItem("roles",roles)
      },
      REFRESH_PAGE:(state) => {
          state.roles  = sessionStorage.getItem("roles") || []
          state.name  = sessionStorage.getItem("name") || ''
          state.loginName  = sessionStorage.getItem("loginName") || ''
          state.phone  = sessionStorage.getItem("phone") || ''
          state.staffId  = sessionStorage.getItem("staffId") || ''
          state.companyId  = sessionStorage.getItem("companyId") || ''
          state.companyName  = sessionStorage.getItem("companyName") || ''
          state.companyLogo  = sessionStorage.getItem("companyLogo") || ''

      },
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const loginName = userInfo.loginName.trim()
      return new Promise((resolve, reject) => {
        loginByLoginName(loginName, userInfo.password).then(response => {
          const resp = response
          commit('SET_TOKEN', resp.data.token)
          setToken(resp.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
          getUserInfo(state.token).then(response => {
              if (!response) { // 由于mockjs 不支持自定义状态码只能这样hack
                  reject('error')
              }
             console.log("===user",response.data);
              // commit('SET_ROLES', response.data.model.role);
              commit('SET_LOGIN_NAME', response.data.model.loginName);
              commit('SET_NAME', response.data.model.name);
              commit('SET_STAFF_ID', response.data.model.id);
              commit('SET_PHONE', response.data.model.phone);
              commit('SET_COMPANY_ID', response.data.model.companyId);
              commit('SET_COMPANY_NAME', response.data.model.companyName);
              commit('SET_COMPANY_LOGO', response.data.model.companyLogo);
              resolve(response)
          }).catch(error => {
              reject(error)
          })
      })
    },
    // 获取用户信息
    RefreshPage({ commit, state }) {
      return new Promise((resolve, reject) => {
          // commit('REFRESH_PAGE');
          resolve(state.staffId === null || state.staffId === '')
      })
    },
    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
          commit('SET_TOKEN', '');
          commit('SET_LOGIN_NAME', '');
          commit('SET_NAME', '');
          commit('SET_ROLES',[]);
          commit('SET_COMPANY_ID',[]);
          removeToken()
          resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          console.log("==response",response);
          const data = response
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
